<?php

function controller_pelanggan($config) {
    if (isset($_SESSION['logged'])){
		if ((isset($config['ruri'][1])) && (isset($config['ruri'][2])) && ($config['ruri'][1])=='id'){
			//ada id
			if ($config['ruri'][2]=='cari'){	//cari
				$qword = $_REQUEST['qword'];
				$off = $_REQUEST['off'];
				$db =  new db($config);
				$delim = 5;
				//
				$result = $db->query("SELECT * FROM mn_customer WHERE nama LIKE '%".urlencode($qword)."%' OR no_hape LIKE '%".urlencode($qword)."%'ORDER BY nama ASC");
				$total = count($result);
				$maxpage = $total / $delim;
				$maxpage = ($maxpage < 1) ? 1 : $maxpage;
				$offset = ($off-1)*$delim;
				$result = $db->query("SELECT * FROM mn_customer WHERE nama LIKE '%".urlencode($qword)."%' OR no_hape LIKE '%".urlencode($qword)."%' OR twitter LIKE '%".urlencode($qword)."%' OR fb LIKE '%".urlencode($qword)."%'ORDER BY nama ASC LIMIT $offset , $delim");
				//
				echo "<div><h5>Terdapat $total pelanggan</h5></div>";
				echo "<ul>";
				if ($result && $db->rowCount() > 0) {
					foreach ($result as $pelanggan){
						echo "<li>";
							echo "<div><a target=\"_blank\" href=\"".$config['home_url']."pelanggan/id/".$pelanggan['idCustomer']."\"><h4><strong>".$pelanggan['nama']."</strong></h4></a></div>";
							echo "<div>Nomor hape : 0".$pelanggan['no_hape']."</div>";
							echo "<div>Alamat : \"".$pelanggan['alamat']."\", ".$pelanggan['kabkot'].", ".$pelanggan['prov']." - ".$pelanggan['kodepos']."</div>";
							echo "<div>Twitter : <a target=\"_blank\" href=\"http://twitter.com/".$pelanggan['twitter']."\">".$pelanggan['twitter']."</a></div>";
							echo "<div>Facebook : <a target=\"_blank\" href=\"http://facebook.com/".$pelanggan['fb']."\">".$pelanggan['fb']."</a></div>";
							echo "<div>Poin : ".$pelanggan['poin']."</div>";
						echo "</li>";
					}
				}
				echo "</ul>";
				if ($off>1)
					echo "<div style=\"float:left; margin-bottom:50px\"><a href=\"javascript:cariPelanggan('".($off-1)."')\">&lt;&lt;Balik</a></div>";
				if ($off < $maxpage)
					echo "<div style=\"float:right; margin-bottom:50px\"><a href=\"javascript:cariPelanggan('".($off+1)."')\">Lanjut&gt;&gt;</a></div>";
				echo "<div style=\"clear:both\"></div>";
			}else{	//view id
				if (isset($_POST['ubah'])){
					$id = $config['ruri'][2];
					$nohp = strtolower($_POST['nohp']);
					$nama = strtolower($_POST['nama']);
					$prov = strtolower($_POST['prov']);
					$kota = strtolower($_POST['kota']);
					$kodepos = strtolower($_POST['pobox']);
					$alamat = strtolower($_POST['lengkap']);
					$fb = strtolower($_POST['fb']);
					$twitter = strtolower($_POST['tw']);
					$db =  new db($config);
					$que = "UPDATE mn_customer SET no_hape='$nohp', nama='$nama', prov='$prov', kabkot='$kota', alamat='$alamat', kodepos='$kodepos', twitter='$twitter', fb='$fb' WHERE idCustomer = '$id'";
					echo $que;
					$db->exec($que);
					echo "<script type=\"text/javascript\">self.close();</script>";
				}else
					view_pelangganid($config,$param,$config['ruri'][2]);
			}
		}else if ((isset($config['ruri'][1])) && (isset($config['ruri'][2])) && ($config['ruri'][1])=='cek'){
			//API cek pelanggan
		}else{
			//daftarkan semua
			view_pelanggan($config,array());
		}
	} else
		header('Location: '.$config['home_url']);
}
